#include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

vector<int> intersect(vector<int> &nums1, vector<int> &nums2) {
    map<int, int> m;
    for (int i = 0; i < nums1.size(); ++i) {
        m.insert(pair<int, int>(nums1[i], i));
    }
    int j = 0;
    for (int i = 0; i < nums2.size(); ++i) {
        if (m.count(nums2[i]) > 0) {
            nums1[j++] = nums2[i];
        }
    }
    if (nums1.size() >= j) { //重新改变向量的大小
        nums1.resize(j - 1);
    }

}
vector<int> intersect1(vector<int> &nums1, vector<int> &nums2) {
    sort(nums1.begin(), nums1.end());
    sort(nums2.begin(), nums2.end());
    int k = 0;
    for(int i = 0, j = 0; i <nums1.size() && j < nums2.size(); ) {
        if(nums1[i] < nums2[j]) {
            ++i;
        } else if(nums1[i] > nums2[j]) {
            ++j;
        } else {
            nums1[k++] = nums1[i];
            ++i;
            ++j;
        }
    }
    nums1.resize(k);
    return nums1;
}

int main() {
    vector<int> nums1 = {1, 2, 2, 1,0};
    vector<int> nums2 = {2, 2};
    intersect1(nums1, nums2);
    for(auto iter = nums1.begin(); iter != nums1.end(); ++iter) {
        cout << *iter << endl;
    }


    //string A("12");
    //string B("12");
    //
    //if (A == B) {
    //    //cout << "hello" << endl;
    //    //cout << A << endl;
    //}
    //
    //auto string1 = "Hello World"; //string1为const char*类型 64位电脑上为8个字节
    //cout << sizeof(string1) << endl;
    //
    //long double d = 2.21L;
    //string s = to_string(d);
    //cout << s << endl;
    //
    //string error = "hello \"World!";
    //string error1 = R"(hello "World!)"; //原始字面量不需要转义
    //cout << error1 << endl;



    return 0;
}